home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / By the Book / Mac C Primer V2 / 4.1 - ColorInfo / ColorInfo.c next >
Encoding:
C/C++ Source or Header  |  1991-08-20  |  3.9 KB  |  198 lines  |  [TEXT/KAHL]

  1. /************************************************************/
  2. /*                                                            */
  3. /*    ColorInfo Code from Chapter Four of                        */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #include "Picker.h"
  15.  
  16. #define BASE_RES_ID            400
  17. #define NIL_POINTER            0L
  18. #define NIL_STRING            "\p"
  19. #define INVISIBLE            FALSE
  20. #define NO_GOAWAY            FALSE
  21. #define MOVE_TO_FRONT        (WindowPtr)-1L
  22. #define REMOVE_ALL_EVENTS    0
  23. #define INDEX_DEVICE        TRUE
  24. #define DIRECT_DEVICE        FALSE
  25.  
  26.  
  27. Boolean IsColor();
  28.  
  29.  
  30. main()
  31. {
  32.     int            pixDepth;
  33.     GDHandle    curDev;
  34.     Rect        bounds;
  35.     
  36.     ToolBoxInit();
  37.     
  38.     if ( IsColor() )
  39.     {
  40.         curDev = GetDeviceList();
  41.         
  42.         while( curDev != NIL_POINTER )
  43.         {
  44.             bounds = (**curDev).gdRect;
  45.         
  46.             pixDepth = GetPixelDepth( curDev );
  47.             switch( pixDepth )
  48.             {
  49.                 case 1:
  50.                     DisplayColors( &bounds, 1, 2, 128, INDEX_DEVICE );
  51.                     break;
  52.                 case 2:
  53.                     DisplayColors( &bounds, 2, 2, 128, INDEX_DEVICE );
  54.                     break;
  55.                 case 4:
  56.                     DisplayColors( &bounds, 4, 4, 64, INDEX_DEVICE );
  57.                     break;
  58.                 case 8:
  59.                     DisplayColors( &bounds, 16, 16, 24, INDEX_DEVICE );
  60.                     break;
  61.                 default:
  62.                     DisplayColors( &bounds, 48, 48, 8, DIRECT_DEVICE );
  63.                     break;
  64.             }
  65.             curDev = GetNextDevice( curDev );
  66.         }
  67.         while( ! Button() ) ;
  68.     }
  69.     else
  70.         DoAlert( "\pThis machine does not support Color QuickDraw!" );
  71. }
  72.  
  73.  
  74. /*********************************** ToolBoxInit */
  75.  
  76. ToolBoxInit()
  77. {
  78.     InitGraf( &thePort );
  79.     InitFonts();
  80.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  81.     InitWindows();
  82.     InitMenus();
  83.     TEInit();
  84.     InitDialogs( NIL_POINTER );
  85.     InitCursor();
  86. }
  87.  
  88.  
  89. /******************************** GetPixelDepth *********/
  90.  
  91. int GetPixelDepth( theDevice )
  92. GDHandle    theDevice;
  93. {
  94.     PixMapHandle    screenPMapH;
  95.     int                pixelDepth;
  96.     
  97.     screenPMapH = (**theDevice).gdPMap;
  98.     pixelDepth = (**screenPMapH).pixelSize;
  99.     return( pixelDepth );
  100. }
  101.  
  102.  
  103. /******************************** IsColor *********/
  104.  
  105. Boolean IsColor()
  106. {
  107.     SysEnvRec    mySE;
  108.     
  109.     SysEnvirons( 1, &mySE );
  110.     return( mySE.hasColorQD );
  111. }
  112.  
  113.  
  114. /*********************************** DisplayColors */
  115.  
  116. DisplayColors( boundsPtr, width, height, pixPerBox, isIndex )
  117. Rect    *boundsPtr;
  118. int        width, height, pixPerBox;
  119. Boolean    isIndex;
  120. {
  121.     Rect        r;
  122.     int            row, col;
  123.     WindowPtr    cWindow;
  124.     RGBColor    curColor;
  125.     HSVColor    hsvColor;
  126.     long        colorNum;
  127.     
  128.     hsvColor.value = hsvColor.saturation = 65535;
  129.     
  130.     r.top = 0;
  131.     r.left = 0;
  132.     r.right = width * pixPerBox;
  133.     r.bottom = height * pixPerBox;
  134.     
  135.     cWindow = NewCWindow( NIL_POINTER, &r, "\pDevice Colors",
  136.             INVISIBLE, noGrowDocProc, MOVE_TO_FRONT,
  137.             NO_GOAWAY, NIL_POINTER );
  138.             
  139.     CenterWindow( cWindow, boundsPtr );
  140.     ShowWindow( cWindow );
  141.     SetPort( cWindow );
  142.     
  143.     for ( row=0; row<height; row++ )
  144.     {
  145.         for ( col=0; col<width; col++ )
  146.         {
  147.             r.top = row * pixPerBox;
  148.             r.left = col * pixPerBox;
  149.             r.bottom = r.top + pixPerBox;
  150.             r.right = r.left + pixPerBox;
  151.             
  152.             if ( isIndex )
  153.                 Index2Color( (long)(row*width + col), &curColor );
  154.             else
  155.             {
  156.                 colorNum = (long)(row*width + col);
  157.                 hsvColor.hue = 65535 * colorNum / (width * height );
  158.                 HSV2RGB( &hsvColor, &curColor );
  159.             }
  160.             RGBForeColor( &curColor );
  161.             PaintRect( &r );
  162.         }
  163.     }
  164. }
  165.  
  166.  
  167. /*********************************** CenterWindow */
  168.  
  169. CenterWindow( w, boundsPtr )
  170. Rect        *boundsPtr;
  171. WindowPtr    w;
  172. {
  173.     Rect        r;
  174.     int            width, height, sWidth, sHeight, h, v;
  175.     
  176.     r = w->portRect;
  177.     
  178.     width = r.right - r.left;
  179.     height = r.bottom - r.top;
  180.     
  181.     sWidth = boundsPtr->right - boundsPtr->left;
  182.     sHeight = boundsPtr->bottom - boundsPtr->top;
  183.     
  184.     h = boundsPtr->left + ((sWidth - width) / 2);
  185.     v = boundsPtr->top + ((sHeight - height) / 2);
  186.     
  187.     MoveWindow( w, h, v, FALSE );
  188. }
  189.  
  190.  
  191. /*********************************** DoAlert */
  192.  
  193. DoAlert( s )
  194. Str255        s;
  195. {
  196.     ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
  197.     NoteAlert( BASE_RES_ID, NIL_POINTER );
  198. }